/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Connection;
import java.sql.*;
/**
 *
 * @author lainh_000
 */
public class SQLConnection {
    String server ="SICHIHIRUKO//SICHIHIRUKO";      //tên server
    String username="sa";     // username
    String password="121291";     //password
    String database="QLSINHVIEN";     //tên database
    
    Connection connect = null;
    Statement statement = null;
    ResultSet result = null;
    
    
    public SQLConnection(String _server,String _username, String _password, String _database)
    {
        server = _server;
        username = _username;
        password = _password;
        database = _database;
    }    
    public SQLConnection(){};
        
    //kiểm tra driver
    protected void driverTest() throws Exception
    {
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        }
        catch (java.lang.ClassNotFoundException e)
        {
            throw new Exception("SQL Server JDBC Driver nót found.");
        }
    }
    
    //tạo connection tới SQL Server
    public  Connection getConnect() throws Exception
    {
        if(connect == null)
        {
            driverTest();
//            String url = "jdbc:sqlserver://" + server + ":1433;" + "databaseName=" +  database;
             String url = "jdbc:sqlserver://localhost:1433;database=QLSACH";
            try
            {
                connect = DriverManager.getConnection(url,username,password);    
            }
            catch (java.sql.SQLException e)
            {
                throw new Exception("Không thể kết nối đến CSDL của bạn.");
            }
        }
        return connect;
    }
    
    //tạo câu query
    protected Statement getStatement() throws Exception
    {
        //kiểm tra xem statement đã đóng hoặc chưa có giá trị
        if(statement == null || statement.isClosed())
        {
            statement = getConnect().createStatement();
        }
        return statement;
    }
    
    //câu query chỉ để đọc
    public ResultSet executeQuery(String query) throws Exception
    {
        try
        {
            result = getStatement().executeQuery(query);
        }
        catch(Exception e)
        {
            throw new Exception("Error: " + e.getMessage() + "-" + query);
        }
        return result;
    }
    
    //câu query làm thay đổi CSDL
    public int executeUpdate(String query) throws Exception
    {
        int res = Integer.MIN_VALUE;
        try
        {
            res = getStatement().executeUpdate(query);
        }
        catch(Exception e)
        {
            throw new Exception("Error: " + e.getMessage() + "-" + query);
        }
        return res;
    }
    
    //đóng connect
    public void CloseConnect() throws SQLException
    {
        //đóng result nếu chưa đóng
        if(result ==  null || result.isClosed()){}
        result.close();
        result=null;
        //đóng statemnent nếu chưa đóng
        if(statement ==  null || statement.isClosed()){}
        statement.close();
        statement=null;
        //đóng connect nếu chưa đóng
        if(connect ==  null || connect.isClosed()){}
        connect.close();
        connect=null;
        
    }
}
